'''
获取福利吧的图片
'''

import urllib.request
import re


''''
打开url
'''
def url_open(url):
    # # 带上参数，让他更像浏览器
    headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0'}
    req = urllib.request.Request(url=url, headers=headers)

    html = urllib.request.urlopen(req).read().decode('utf-8')
    print("网页源码：" + html)
    return html

'''
获取图片的地址
'''
def get_img(html):
    # r是正则的标识，<p><strong><img src=" 表示要匹配这个，【^"】表示不包含"，表示出现一次或多次， .jpg结尾
    p = r'<p><strong><img src="([^"]+\.[gjp][ipn][fpg])"'
    imgList = re.findall(p, html)

    # 打印出图片地址
    for each in imgList:
        print("图片地址：" + each)

    for each in imgList:
        filename = each.split("/")[-1]
        print("t图片地址：" + str(filename))
        urllib.request.urlretrieve(each, filename, None)


url = "https://www.biqiuge.com/book/14184/"
# html = url_open(url)
# get_img(html)



# # 带上参数，让他更像浏览器
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0'}
req = urllib.request.Request(url=url, headers=headers)
s = urllib.request.urlopen(req).read()

from version1 import webUtils

encode = webUtils.getEncode(s)
print(encode)
s.decode(encode)
print(s)
y = webUtils.getEncode(s)
print(y)
# html = urllib.request.urlopen(req).read().decode('utf-8')
# print("网页源码：" + html)
